package codeflyer.server.utils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import codeflyer.server.classload.ClassLoad;

/**
 * @author CodeFlyer
 * @date May 7, 2020
 * @version 1.0
 */

public class PropertiesAnalysis {
	
	private static Map<String,String> propertiesMap = null;
	
	/**
	 * 返回值是一个map,key是项目绝对路径和对应的端口号
	 * @return
	 */
	public static Map<String, String> getPMap(){
		if(propertiesMap != null) {
			return propertiesMap;
		}
		InputStream is = null;
		propertiesMap = new HashMap<>();
		try {
			for (String string : ClassLoad.getJarFolderList()) {
				string = string.substring(0, string.lastIndexOf("/"));  //去除最后一个斜杆
				is = new BufferedInputStream(new FileInputStream(string+"/web_content"+"/web.properties"));
				Properties properties = new Properties();
				properties.load(is);
				propertiesMap.put(string, properties.getProperty("server.port"));
				propertiesMap.put("exclude", properties.getProperty("exclude.html"));
			}
			return propertiesMap;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {			
			try {
				if(is!=null) {
					is.close();
				}				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}


}


